﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.RDW.Model;
using Wicresoft.RDW.Interface;
using System.Collections;
using System.Configuration;

namespace Wicresoft.RDW.Presentation
{
    public class PriviledgePresent : BasePresent<Priviledge, IPriviledge>
    {
        protected const int AllUser = -1;
        public static bool PathEnabled(string path, int userID)
        {
            bool result = false;
            if (!PathExists(path))//Path中没有配置权限
            {
                result = true;
            }
            else
            {
                if (userID > 0)
                {
                    if (UserPathEnabled(path, userID))//Path配置了人的权限
                    {
                        result = true;
                    }
                    else//Path配置了角色权限
                    {
                        //循环判断这个人或者角色是否有这个权限
                        if (RolePathEnabled(path, userID))
                        {
                            result = true;
                        }
                    }
                }
            }
            return result;

        }
        /// <summary>
        /// 判断角色是否有这个权限
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        public static bool RolePathEnabled(string path, int userId)
        {
            var priviledge = present.GetList(p => p.PagePath == path);
            foreach (var p in priviledge)
            {
                List<UserRole> list = UserRolePresent.GetList(ur => ur.RoleId == p.RoleId);
                foreach (UserRole role in list)
                {
                    if (role.UserId == userId || role.UserId==AllUser)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
       
        /// <summary>
        /// 判断用户是否有这个权限
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        public static bool UserPathEnabled(string path, int userId)
        {
            var m = present.GetList(p => p.PagePath == path);
            foreach (var item in m)
            {
                if (item.UserId == userId)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 路径配置不存在则表示该页面有权限
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool PathExists(string path)
        {
            List<Priviledge> pList = present.GetList(p => p.PagePath == path);
            if (pList.Count > 0)
            {
                return true;
            }
            return false;
        }

        public static bool IsCurrentLabManager(int userId, int labId)
        {
            return IsLabManagerOrIsCurrentLabManager(userId, labId, 0);
        }

        public static bool IsLabManager(int userId)
        {
            return IsLabManagerOrIsCurrentLabManager(userId, 0, 1);
        }

        public static bool IsSiteManager(int userId)
        {
            bool result = false;
            var list = UserRolePresent.GetList(p => p.UserId == userId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    if (ListNotNull(SiteManagerPresent.GetList(p => p.RoleId == item.RoleId)))
                    {
                        result = true;
                        break;
                    }
                }
            }
            return result;
        }
        public static bool ListNotNull(IList list)
        {
            if (list != null && list.Count > 0)
                return true;
            else
                return false;
        }
        public static bool IsAdmin(int userId)
        {
            return IsExists(userId, SysSettingPresent.AdminRoleId);
        }
        public static bool IsCenterManager(int userId)
        {
            return IsExists(userId, SysSettingPresent.CenterManagerRoleId);
        }
        protected static bool IsExists(int userId, int roleId)
        {
            bool result = false;
            var list = UserRolePresent.GetList(p => p.UserId == userId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    if (item.RoleId == roleId)
                    {
                        result = true;
                        break;
                    }
                }
            }
            return result;
        }
        private static bool IsLabManagerOrIsCurrentLabManager(int userId, int labId, int type)
        {
            bool result = false;
            var list = UserRolePresent.GetList(p => p.UserId == userId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    var labManagerList = LabManagerPresent.GetList(p => p.RoleId == item.RoleId);
                    if (ListNotNull(labManagerList))
                    {
                        switch (type)
                        {
                            case 0:
                                foreach (var labManager in labManagerList)
                                {
                                    if (labId == labManager.LabId)
                                    {
                                        result = true;
                                        break;
                                    }
                                }
                                break;
                            case 1:
                                result = true;
                                break;
                        }

                    }
                }
            }
            return result;
        }
        public static bool IsEndUser(int userId)
        {

            var endUserRoleId = int.Parse(ConfigurationManager.AppSettings["EndUserRoleId"]);
            var userRoles = UserRolePresent.GetList(x => x.RoleId == endUserRoleId);
            foreach (var userRole in userRoles)
            {
                if (AllUser == userRole.UserId || userId == userRole.UserId)
                {
                    return true;
                }
            }
            return false;

        }
    }

}
